home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 176-200 / disk_179 / launch / wblaunch.c < prev   
C/C++ Source or Header  |  1992-05-06  |  3KB  |  141 lines

  1. #include <exec/memory.h>
  2. #include <workbench/startup.h>
  3. #include <workbench/workbench.h>
  4.  
  5. UBYTE *AllocMem();
  6. BPTR LoadSeg();
  7. struct MsgPort *CreateProc();
  8.  
  9. /* Launch(rport, name, argv, argc, pri, win, stack) -- launch a program.
  10.  *
  11.  * Launch builds a startup message, loads a program, and launches it.
  12.  *
  13.  * Arguments:
  14.  *    rport -- reply port to receive startup message back when the program
  15.  *             finishes.
  16.  *    name  -- File name for the program to load.
  17.  *    argv  -- list of filenames to pass to loaded program.
  18.  *    argc  -- length of argument list.
  19.  *    pri   -- priority of the new process.
  20.  *    win   -- ToolWindow for the new process, or NULL.
  21.  *    stack -- Stack size for the new process.
  22.  *
  23.  * argv[0] should be the same as the program. pri should normally be 0.
  24.  * stack should normally be at least 4K.
  25.  */
  26. struct WBStartup *
  27. Launch(rport, name, argv, argc, pri, win, stack)
  28. struct MsgPort *rport;
  29. char *name;
  30. char **argv;
  31. int argc;
  32. long pri;
  33. char *win;
  34. ULONG stack;
  35. {
  36.     ULONG flock;
  37.     struct WBStartup *msg;
  38.     char *s, *namep;
  39.     int i;
  40.  
  41.     if(!rport)
  42.         return 0;
  43.  
  44.     /* Get some space to work in -- the startup message */
  45.     msg = (struct WBStartup *)
  46.         AllocMem(sizeof(struct WBStartup), MEMF_PUBLIC|MEMF_CLEAR);
  47.     if(!msg)
  48.         return 0;
  49.  
  50.     /* Now load the program */
  51.     msg->sm_Segment = LoadSeg(name);
  52.     if(!msg->sm_Segment) {
  53.         FreeStartup(msg);
  54.         return 0;
  55.     }
  56.  
  57.     /* Allocate and link in the window description */
  58.     if(win) {
  59.         msg->sm_ToolWindow = (char *)AllocMem(strlen(win)+1, MEMF_PUBLIC);
  60.         if(!msg->sm_ToolWindow) {
  61.             FreeStartup(msg);
  62.             return 0;
  63.         }
  64.         strcpy(msg->sm_ToolWindow, win);
  65.     } else
  66.         msg->sm_ToolWindow = 0;
  67.  
  68.     /* Allocate the arg list */
  69.     msg->sm_ArgList = (struct WBArg *)
  70.         AllocMem(sizeof(struct WBArg) * argc, MEMF_PUBLIC | MEMF_CLEAR);
  71.     if(!msg->sm_ArgList) {
  72.         FreeStartup(msg);
  73.         return 0;
  74.     }
  75.  
  76.     /* Empty out all args, just in case this aborts, so cleanup
  77.      * can clean it up
  78.      */
  79.     msg->sm_NumArgs = argc;
  80.     for(i = 0; i < argc; i++) {
  81.         msg->sm_ArgList[i].wa_Lock = 0;
  82.         msg->sm_ArgList[i].wa_Name = 0;
  83.     }
  84.  
  85.     /* Get lock and name for wbargs */
  86.     for(i = 0; i < argc; i++) {
  87.         flock = Lock(argv[i]);
  88.         if(!flock) {
  89.             FreeStartup(msg);
  90.             return 0;
  91.         }
  92.         msg->sm_ArgList[i].wa_Lock = ParentDir(flock);
  93.         UnLock(flock);
  94.         if(!msg->sm_ArgList[i].wa_Lock) {
  95.             FreeStartup(msg);
  96.             return 0;
  97.         }
  98.  
  99.         /* Get the tail end of the file name. I could also do an Examine
  100.          * on the lock, but didn't want to bother allocating a File Info
  101.          * Block that wouldn't fit under the tracking via the startup
  102.          * message.
  103.          */
  104.         namep = argv[i];
  105.         for(s = argv[i]; *s; s++)
  106.             if(*s=='/' || *s==':')
  107.                 namep = s+1;
  108.         msg->sm_ArgList[i].wa_Name =
  109.             (char *)AllocMem(strlen(namep)+1, MEMF_PUBLIC);
  110.         if(!msg->sm_ArgList[i].wa_Name) {
  111.             FreeStartup(msg);
  112.             return 0;
  113.         }
  114.         strcpy(msg->sm_ArgList[i].wa_Name, namep);
  115.     }
  116.  
  117.     /* Create the process. It is now running, but will wait for the
  118.      * startup message.
  119.      */
  120.     msg->sm_Process =
  121.         CreateProc(msg->sm_ArgList[0].wa_Name, pri, msg->sm_Segment, stack);
  122.     if(!msg->sm_Process) {
  123.         FreeStartup(msg);
  124.         return 0;
  125.     }
  126.  
  127.     /* Initialise the message part of the startup message, and pass it to
  128.      * the process. At this point it will actually start ding work
  129.      */
  130.     msg->sm_Message.mn_ReplyPort = rport;
  131.     msg->sm_Message.mn_Length = sizeof(struct WBStartup);
  132.     msg->sm_Message.mn_Node.ln_Type = NT_MESSAGE;
  133.  
  134.     PutMsg(msg->sm_Process, msg);
  135.  
  136.     /* return message. Not very useful, but it's as meaningful a response as
  137.      * any.
  138.      */
  139.     return msg;
  140. }
  141.